package com.bsb.biz.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bsb.base.common.util.ShortUuid;
import com.bsb.base.mapper.BatchCardMapper;
import com.bsb.base.mapper.BsbCardBatchMapper;
import com.bsb.base.model.BsbCard;
import com.bsb.base.model.BsbCardBatch;
import com.bsb.base.model.BsbCardBatchExample;

@Service
public class BsbCardBatchService{
	
	@Resource
	private BsbCardBatchMapper bsbCardBatchMapper;
	
	@Resource
	private BatchCardMapper batchCardMapper;
	
	public void addBatchCard(Long num , Long firmId){
		Long lastId = batchCardMapper.getLastId();
		if(null == lastId){
			lastId = 0L;
		}
		Long start = lastId +1 ;
		Long end = lastId + num;
		BsbCardBatch batch = new BsbCardBatch();
		batch.setCreateDate(new Date());
		batch.setStartNo(start);
		batch.setEndNo(end);
		batch.setFirmId(firmId);
		batch.setNum(num);
		bsbCardBatchMapper.insertSelective(batch);
		ArrayList<BsbCard> cards = new ArrayList<BsbCard>();
		for (int i = 0; i < num; i++) {
			BsbCard card = new BsbCard();
			card.setBatchId(batch.getId());
			card.setCreateDate(new Date());
			card.setFirmId(firmId);
			card.setSecurityCode(ShortUuid.generateShortUuid());
			card.setStatus("0");
			cards.add(card);
		}
		HashMap<String, ArrayList<BsbCard>> map = new HashMap<>();
		map.put("batchCard", cards);
		batchCardMapper.insertBatchCard(map);
	}
	
	public void addBsbCardBatch(BsbCardBatch bsbCardBatch) {
		bsbCardBatchMapper.insertSelective(bsbCardBatch);
	}
	
	public void delBsbCardBatchById(Long id) {
		bsbCardBatchMapper.deleteByPrimaryKey(id);
	}
	
	public void updateBsbCardBatch(BsbCardBatch bsbCardBatch) {
		bsbCardBatchMapper.updateByPrimaryKeySelective(bsbCardBatch);
	}
	
	public void updateBsbCardBatchByExample(BsbCardBatch bsbCardBatch,BsbCardBatchExample bsbCardBatchExample){
	    bsbCardBatchMapper.updateByExampleSelective(bsbCardBatch,bsbCardBatchExample);
	}
	
	public BsbCardBatch getBsbCardBatchById(Long id) {
		return bsbCardBatchMapper.selectByPrimaryKey(id);
	}
	
	public List<BsbCardBatch> getPageBsbCardBatch(BsbCardBatchExample bsbCardBatchExample) {
		return bsbCardBatchMapper.selectPage(bsbCardBatchExample);
	}
	
	public int countBsbCardBatch(BsbCardBatchExample bsbCardBatchExample) {
		return bsbCardBatchMapper.countByExample(bsbCardBatchExample);
	}
	
	public List<BsbCardBatch> selectByExample(BsbCardBatchExample example){
	    return bsbCardBatchMapper.selectByExample(example);
	}
}
